package io.imp.modules.tasks.unionqrc.service;

import io.imp.modules.sys.service.SysDictService;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayRefundJnl;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayTxnJnl;
import io.imp.modules.tasks.unionqrc.entity.ItcsUnionpayTxnJnlPart;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

/**
 * 功能描述:
 * <退货文件数据处理>
 *
 * @ClassName: WorkItcsUnionpayReverseJnl
 * @Date: 2019年05月17日 13:31:04
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
public class WorkItcsUnionpayRefundJnl implements Callable {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private ItcsUnionpayRefundJnlService itcsUnionpayRefundJnlService;

    private ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService;

    private SysDictService sysDictService;

    private ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService;

    private List<ItcsUnionpayRefundJnl> source;

    private CountDownLatch countDownLatch;
    public WorkItcsUnionpayRefundJnl(ItcsUnionpayRefundJnlService itcsUnionpayRefundJnlService,ItcsUnionpayTxnJnlService itcsUnionpayTxnJnlService,SysDictService sysDictService,ItcsUnionpayTxnJnlPartService itcsUnionpayTxnJnlPartService,List<ItcsUnionpayRefundJnl> source,CountDownLatch countDownLatch){
        this.itcsUnionpayRefundJnlService = itcsUnionpayRefundJnlService;
        this.itcsUnionpayTxnJnlService = itcsUnionpayTxnJnlService;
        this.sysDictService = sysDictService;
        this.itcsUnionpayTxnJnlPartService = itcsUnionpayTxnJnlPartService;
        this.source = source;
        this.countDownLatch = countDownLatch;
    }
    @Override
    public String call() throws Exception {
        logger.info("退款流水数据处理...");
        List<ItcsUnionpayTxnJnlPart> itcsUnionpayTxnJnlParts = new LinkedList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        Calendar calendar = Calendar.getInstance();

        StringBuilder result = new StringBuilder();
        if (source.size()!=0){

            for (ItcsUnionpayRefundJnl refund:source
                    ) {

                String origqryid = refund.getOrigqryid();
                ItcsUnionpayTxnJnl itcsUnionpayTxnJnl = itcsUnionpayTxnJnlService.getItcsUnionpayTxnJnlWithOne(new String[] { "queryid" }, new Object[] {origqryid});
                String txnno = itcsUnionpayTxnJnl.getTxnno();
                String orderid = itcsUnionpayTxnJnl.getOrderid();

                String txntype = refund.getTxntype();
                txntype = StringUtils.isBlank(txntype)?"04":txntype;
                String txnsubtype = refund.getTxnsubtype();
                txnsubtype = StringUtils.isBlank(txnsubtype)?"00":txnsubtype;
                result.append(refund.getMerid()).append("|").append(refund.getTermId()).append("|").
                        append("").append("|").append(refund.getOrderid()).append("|").
                        append(refund.getTxntime()).append("|").append("").append("|").
                        append(refund.getTxnamt()).append("|").append(txntype).append("|").
                        append(txnsubtype).append("|").append(refund.getTraceno()).append("|").
                        append(refund.getRespcode()).append("|").append(refund.getRespmsg()).
                        append("|").append(refund.getSettledate()).append("|").append(refund.getSettleamt()).
                        append("|").append(itcsUnionpayTxnJnl.getAccno()).append("|").
                        append(itcsUnionpayTxnJnl.getOrderid()).append("|").append("消费退货").append("\r\n");
                itcsUnionpayRefundJnlService.update("1",refund.getTxnno());
            }
        }else {
            result.append("");
        }
        this.countDownLatch.countDown();
        return result.toString().replace("null","");
    }
}
